package com.fishV.BI.utils;

import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.excel.EasyExcel;
import io.netty.util.internal.ResourcesUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: Excel工具类
 * @author yuwei
 * @date 15:13 2024/10/18
 */
public class ExcelUtils {
    public static String readExcel(MultipartFile file){


        // 这里 也可以不指定class，返回一个list，然后读取第一个sheet 同步读取会自动finish
        List<Map<Integer, String>> listMap = null;
        try {
            listMap = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(0).doReadSync();
        } catch (IOException e) {
            return "读取文件失败";
        }
        for (Map<Integer, String> data : listMap) {
            System.out.println(data.values());
        }
        if(listMap.size()==0){
            return "excel文件为空";
        }
        StringBuilder builder = new StringBuilder();
        //表头
        Map<Integer,String> headMap = (LinkedHashMap)listMap.get(0);
        List<String> headList = headMap.values().stream().filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
        builder.append(StringUtils.join(headList,","));
        builder.append("\n");
        for(Map<Integer, String> data : listMap){
            builder.append(StringUtils.join(data.values(),","));
            builder.append("\n");
        }
        return builder.toString();
    }


}
